<?php
namespace Model;

use Core\Constant;
use Core\Service\Parameterchooser\ParamableInterface;
use Core\Session\AdminSessionHelper;
use Model\Factory\TableGatewayFactory;

class Article extends AbstractModel implements ParamableInterface
{
    public $id;
    public $content;
    public $header;
    public $thumbnail;
    public $sumarize;
    public $image;
    public $order_number;
    public $alias;
    public $created_date;
    public $created_by;
    public $active;
    public $user_name;


    /**
     * @return string
     */
    public function getSpecification()
    {
        return $this->header;
    }

    /**
     * @return string
     */
    public function getParameter()
    {
        return $this->alias.'_'.$this->id;
    }

    public function detachAllCategory(){
        $categorySet = $this->getAllCategory();
        foreach ($categorySet as $articleCategory) {
            $articleCategoryTable = new ArticleCategoryTable();
            $articleCategoryTable->delete($articleCategory->id);
        }
    }

    /**
     * @return array
     */
    public function getAllCategory()
    {

        $categoryTable = TableGatewayFactory::getCategoryTable(AdminSessionHelper::getCurrentLang());
        $select = $categoryTable->getDbSqlSelect();
        $select->join(array('ur'=>'article_category'), 'ur.category_id = category.id', array())
            ->where(array('ur.article_id'=>$this->id));
        $rsi = $categoryTable->getTableGateway()->selectWith($select);
        $resultArr = array();
        foreach ($rsi as $aCategory) {
            array_push($resultArr, $aCategory);
        }

        return $resultArr;
    }

    public function addCategory($categoryId)
    {

        $articleCategoryTable = TableGatewayFactory::getArticleCategoryTable();
        $articleCategoryTable->insert(array("article_id"=>$this->id,"category_id"=>$categoryId));
    }

    /**
     * @return Category
     */
    public function getPrimaryCategory(){
        $arrCat = $this->getCategoryByGroupCode(Constant::$CATEGORY_GROUP_ARTICLE);
        if ($arrCat != null && $arrCat)
            return $arrCat;
        else
            return null;
    }

    /**
     * @return array
     */
    public function getSubCategories(){
        return $this->getCategoryByGroupCode(Constant::$CATEGORY_GROUP_SUBARTICLE);
    }

    /**
     * @param $groupCode
     * @return array
     */
    public function getCategoryByGroupCode($groupCode)
    {

        $categoryTable = TableGatewayFactory::getCategoryTable(AdminSessionHelper::getCurrentLang());
        $select = $categoryTable->getDbSqlSelect();
        $select->join(array('ur' => 'article_category'), 'ur.category_id = category.id', array())
            ->where(array('ur.article_id' => $this->id, 'category.group_code' => $groupCode));
        $rsi = $categoryTable->getTableGateway()->selectWith($select);
        $resultArr = array();
        foreach ($rsi as $aCategory) {
            array_push($resultArr, $aCategory);
        }

        return $resultArr;
    }

    /**
     * @return array
     */
    public function getTags()
    {
        $tagTable = TableGatewayFactory::getTagTable(AdminSessionHelper::getCurrentLang());
        $select = $tagTable->getDbSqlSelect();
        $select->join(array('ur' => 'article_tag'), 'ur.tag_id = tag.id', array())
            ->where(array('ur.article_id' => $this->id));
        $rsi = $tagTable->getTableGateway()->selectWith($select);
        $resultArr = array();
        foreach ($rsi as $aCategory) {
            array_push($resultArr, $aCategory);
        }

        return $resultArr;
    }
}
